/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.thinkgem.jeesite.common.utils;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.jeecg.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;



/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 *
 * @author ThinkGem
 * @version 2013-3-15
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    static Logger log = LoggerFactory.getLogger(DateUtils.class);
    private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
            "yyyy.MM.dd HH:mm", "yyyy.MM" };

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd）
     */
    public static String getDate() {
        return getDate("yyyy-MM-dd");
    }

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String getDate(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    /**
     * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String formatDate(Date date, Object... pattern) {
        if (date == null) {
            return null;
        }
        String formatDate = null;
        if (pattern != null && pattern.length > 0) {
            formatDate = DateFormatUtils.format(date, pattern[0].toString());
        } else {
            formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
        }
        return formatDate;
    }

    /**
     * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String formatDateTime(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 得到当前时间字符串 格式（HH:mm:ss）
     */
    public static String getTime() {
        return formatDate(new Date(), "HH:mm:ss");
    }

    /**
     * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String getDateTime() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 得到当前年份字符串 格式（yyyy）
     */
    public static String getYear() {
        return formatDate(new Date(), "yyyy");
    }

    /**
     * 得到当前月份字符串 格式（MM）
     */
    public static String getMonth() {
        return formatDate(new Date(), "MM");
    }

    /**
     * 得到当天字符串 格式（dd）
     */
    public static String getDay() {
        return formatDate(new Date(), "dd");
    }

    /**
     * 得到当前星期字符串 格式（E）星期几
     */
    public static String getWeek() {
        return formatDate(new Date(), "E");
    }

    /**
     * 日期型字符串转化为日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
     * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy.MM.dd",
     * "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
     */
    public static Date parseDate(Object obj) {
        if (obj == null) {
            return null;
        }
        String str = obj.toString().trim();
        if(str.length()<4) return null;
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            log.error("转日期异常:"+str.toString(), e);
            return null;
        }
    }

    /**
     * 获取过去的天数
     *
     * @param date
     * @return
     */
    public static long pastDays(Date date) {
        long t = new Date().getTime() - date.getTime();
        return t / (24 * 60 * 60 * 1000);
    }

    public static Date getDateStart(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(formatDate(date, "yyyy-MM-dd") + " 00:00:00");
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return date;
    }

    public static Date getDateEnd(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(formatDate(date, "yyyy-MM-dd") + " 23:59:59");
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return date;
    }

    public static Date D2Formate(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = sdf.parse(formatDate(date, "yyyy-MM-dd"));
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return date;
    }

    public static Date getNowDate(String datas) {
        if (StringUtils.isEmpty(datas)) {
            return null;
        }
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        Date date = null;
        try {
            date = format.parse(datas);
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return date;
    }

    public static Date addDate(Date date, long day) {
        long time = date.getTime(); // 得到指定日期的毫秒数
        day = day * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
        time += day; // 相加得到新的毫秒数
        return new Date(time); // 将毫秒数转换成日期
    }

    public static Date S2D(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Date now = null;
        try {
            now = sdf.parse(date);
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        ;
        return now;
    }

    public static String getWeek(Date date) {
        String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }

    public static int getWeekNo(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return week_index;
    }

    public static String D2ForMat(LocalDate date, String formatter) {// jdk 8
        // 日期格式化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatter);
        return date.format(dateTimeFormatter);
    }

    public static Date getCurrentYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-01-01");
        Date date = new Date();
        try {
            return parseDate(sdf.format(date), "yyyy-MM-dd");
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static int daysBetween(Date one, Date two) {
        int days = (int) ((one.getTime() - two.getTime()) / 86400000);
        return days;
    }

    public static int longOfTwoDate(Date first, Date second) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(first);
        int cnt = 0;
        while (calendar.getTime().compareTo(second) != 0) {
            calendar.add(Calendar.DATE, 1);
            cnt++;
        }
        return cnt;
    }

    public static long marginTime(Date from, Date to) {
        if (from == null) {
            return 0;
        }
        if (to == null) {
            return 0;
        }
        Calendar caFrom = Calendar.getInstance();
        caFrom.setTime(from);
        Calendar caTo = Calendar.getInstance();
        caTo.setTime(to);
        if (caFrom.compareTo(caTo) > 0) {
            return caFrom.getTimeInMillis() - caTo.getTimeInMillis();
        } else {
            return caTo.getTimeInMillis() - caFrom.getTimeInMillis();
        }
    }

    public static double getHousFor2Date(Date from, Date to) {
        if (from == null) {
            return 0;
        }
        if (to == null) {
            return 0;
        }
        Calendar caFrom = Calendar.getInstance();
        caFrom.setTime(from);
        Calendar caTo = Calendar.getInstance();
        caTo.setTime(to);
        double sum = 0.0;
        if (caFrom.compareTo(caTo) > 0) {
            sum = (caFrom.getTimeInMillis() - caTo.getTimeInMillis());
        } else {
            sum = (caTo.getTimeInMillis() - caFrom.getTimeInMillis());
        }
        sum = getTwoDecimal(sum / (1000 * 60 * 60));
        return sum;
    }

    /**
     * 将数据保留两位小数
     */
    public static double getTwoDecimal(double num) {
        DecimalFormat dFormat = new DecimalFormat("#.0");
        String yearString = dFormat.format(num);
        Double temp = Double.valueOf(yearString);
        return temp;
    }

    public static String dateToStr(long between) {
        // 天
        int day = (int) Math.floor(between / (1000 * 60 * 60 * 24));
        // 时
        int hour = (int) Math.floor(between / (1000 * 60 * 60) - (day * 24));
        // 分
        int min = (int) Math.floor(between / (1000 * 60) - (day * 24 * 60) - (hour * 60));
        String dayStr = "";
        String hourStr = "";
        String minStr = "";
        if (day > 0) {
            dayStr = day + "天";
        }
        if (hour > 0) {
            hourStr = hour + "小时";
        }
        if (min > 0) {
            minStr = min + "分钟";
        }
        return dayStr + hourStr + minStr;
    }

    // 获取开始到结束日期的月份
    public static List<String> getMonthBetween(String minDate, String maxDate) throws Exception {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");// 格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }

    public static Date str2Date(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return format.parse(date);
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static Date str2Date(String date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static String detailStr(Date date) {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
    }

    public static List<Date> getDateList(Date startDate, Date endDate) {

        List<Date> list = Lists.newArrayList();

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(startDate); // 重新设置一下日期

        while (calendar.getTime().compareTo(endDate) <= 0) { // 根据当前的日期做查询

            list.add(calendar.getTime());

            calendar.add(Calendar.DATE, 1);
        }

        return list;
    }

    public static Date getSomeDate(int year, int month) {// 传年月 获取当前的某天
        Calendar ca = Calendar.getInstance();
        ca.clear();
        ca.set(ca.YEAR, year);
        ca.set(ca.MONTH, month - 1);
        Calendar calendar = Calendar.getInstance();
        calendar.set(ca.YEAR, year);
        calendar.set(ca.MONTH, month - 1);
        if (DateUtils.str2Date(year + "-" + month + "-" + ca.getActualMaximum(ca.DAY_OF_MONTH))
                .compareTo(calendar.getTime()) < 0) {
            return (DateUtils.str2Date(year + "-" + month + "-" + ca.getActualMaximum(ca.DAY_OF_MONTH)));// 获取当前的日期
        } else {
            return (calendar.getTime());
        }
    }

    //两个日期相减得到的天数
    public static int getDiffDays(Date beginDate, Date endDate)
    {

        if (beginDate == null || endDate == null) {
            throw new IllegalArgumentException("getDiffDays param is null!");
        }

        long diff = (endDate.getTime() - beginDate.getTime())
                / (1000 * 60 * 60 * 24);

        int days = new Long(diff).intValue();

        return days;
    }

    // 获取指定月份的天数
    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    // 获取指定月份的最小，最大日期
    public static List<Date> getMinAndMaxDay(Date date) {
        ArrayList<Date> result = new ArrayList<Date>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        Date maxDate = cal.getTime();
        Date minDate = str2Date(year + "-" + month + "-01");
        result.add(minDate);
        result.add(maxDate);
        return result;

    }

    // 指定日期获取开始到结束日期的月份
    public static List<Date> getMonthAllDay(Date date) {
        ArrayList<Date> result = new ArrayList<Date>();

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        int m = cal.get(Calendar.MONTH);
        int dayNumOfMonth = getDaysByYearMonth(year, m);
        // 从一号开始
        cal.set(Calendar.DAY_OF_MONTH, 1);

        for (int i = 0; i < dayNumOfMonth; i++, cal.add(Calendar.DATE, 1)) {
            Date d = cal.getTime();
            /*
             * SimpleDateFormat simpleDateFormat = new
             * SimpleDateFormat("yyyy-MM-dd"); String df =
             * simpleDateFormat.format(d);
             */
            result.add(d);
        }

        return result;
    }

    // 指定日期获取开始到结束日期的月份
    public static List<Date> getMonthAllDays(Date date) {
        ArrayList<Date> result = new ArrayList<Date>();
        if (getMinAndMaxDay(date).size() <= 1) {
            return result;
        }
        Calendar calendar = Calendar.getInstance();
        Date startDate = getMinAndMaxDay(date).get(0);
        calendar.setTime(startDate);
        Date endDate = getMinAndMaxDay(date).get(1);

        while (calendar.getTime().compareTo(endDate) <= 0) {
            result.add(calendar.getTime());
            calendar.add(Calendar.DATE, 1);
        }
        return result;
    }

    public static String getSomeDayWithInt(int i) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, i);
        Date time = cal.getTime();
        return (new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(time));
    }

    public static String getLast12Months(int i) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -i);
        Date m = c.getTime();
        return sdf.format(m);
    }

    // 获取下个月的日期
    public static String getFirstDayOfNextMonth(String dateStr) {
        DateFormat format2 = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        try {
            date = format2.parse(dateStr);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        c.add(Calendar.MONTH, 1);
        String time = format.format(c.getTime());
        return time;
    }

    // 根据String类型的date格式获取到对应的月份1号和天数
    public static String getFirstDay(String dateStr) {
        DateFormat format2 = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        try {
            date = format2.parse(dateStr);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        c.add(Calendar.MONTH, 1);
        String time = format.format(c.getTime());
        return time;
    }

    // 获取月份的天数
    public static Integer getDayByDate(String dateStr) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateStr));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            log.error(e.getMessage(), e);
        }
        int day = cal.get(Calendar.DATE);
        return day;
    }

    /**
     * 数据格式 是 yyyy-mm
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getMonthList(int count, String endDate) {
        List<String> list = Lists.newArrayList();
        try {

            Date d2 = new SimpleDateFormat("yyyy-MM").parse(endDate);// 定义结束日期可以去当前月也可以手动写日期。

            Calendar dd = Calendar.getInstance();// 定义日期实例

            dd.setTime(d2);// 设置日期起始时间

            int i = 0;

            while (i < count) {// 判断是否到结束日期

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

                String str = sdf.format(dd.getTime());

                list.add(str);

                dd.add(Calendar.MONTH, -1);

                i++;

            }
            return list;

        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }

        return null;
    }

    /**
     * 获取年份列表 到当前的年份
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getYearList() {
        List<String> list = Lists.newArrayList();
        try {

            SimpleDateFormat format = new SimpleDateFormat("yyyy");

            String nowdate = format.format(new Date());// 当前月份

            Date d1 = new SimpleDateFormat("yyyy").parse("2017");// 定义起始日期

            Date d2 = new SimpleDateFormat("yyyy").parse(nowdate);// 定义结束日期可以去当前月也可以手动写日期。

            Calendar dd = Calendar.getInstance();// 定义日期实例

            dd.setTime(d1);// 设置日期起始时间

            while (dd.getTime().before(d2)) {// 判断是否到结束日期

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

                String str = sdf.format(dd.getTime());

                list.add(str);

                dd.add(Calendar.YEAR, 1);// 进行当前日期月份加1

            }

            list.add(formatDate(d2, "yyyy"));// 输出日期结果

            return list;

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return null;
    }

    /**
     * 获取汉字型月份
     *
     * @param month
     * @return
     */
    public static String getWordMonth(String month) {
        String value = "";
        switch (month) {
            case "01":
                value = "一";
                break;
            case "02":
                value = "二";
                break;
            case "03":
                value = "三";
                break;
            case "04":
                value = "四";
                break;
            case "05":
                value = "五";
                break;
            case "06":
                value = "六";
                break;
            case "07":
                value = "七";
                break;
            case "08":
                value = "八";
                break;
            case "09":
                value = "九";
                break;
            case "10":
                value = "十";
                break;
            case "11":
                value = "十一";
                break;
            case "12":
                value = "十二";
                break;
            default:
                value = "一";
                break;
        }
        return value + "月份";
    }

    /**
     * 获取两个日期之间的相处月份
     *
     * @param minDate
     * @param maxDate
     * @return
     * @throws ParseException
     */
    public static List<String> getMonthBetweenTwoDate(String minDate, String maxDate) {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");// 格式化为年月
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        try {
            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
            return result;
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    // 四舍五入把double转化int整型，0.5进一，小于0.5不进一
    public static int getInt(double number) {
        BigDecimal bd = new BigDecimal(number).setScale(0, BigDecimal.ROUND_HALF_UP);
        return Integer.parseInt(bd.toString());
    }

    public static int getGapCount(Date startDate, Date endDate) {
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(startDate);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(endDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);

        return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
    }

    /**
     * 获得指定日期的后一天
     *
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);

        String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayAfter;
    }

    /**
     * 获得指定日期的后一天返回date类型
     *
     * @param specifiedDay
     * @return
     */
    public static Date getDateDayAfter(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获得指定日期的前一天返回date类型
     *
     * @param specifiedDay
     * @return
     */
    public static Date getDateDayBefore(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        date = calendar.getTime();
        return date;
    }
    /**
     * 指定日期加上天数后的日期
     * @param num 为增加的天数
     * @param newDate 创建时间
     * @return
     * @throws ParseException
     */
    public static Date plusDay(int num,String createDate) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(createDate);
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.DATE, num);
        String temp = "";
        temp = sdf.format(cl.getTime());
        date = sdf.parse(temp);
        return date;
    }

    /**
     * 时间戳转换时间
     * @param s
     * @return
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


    public static Date str2Date2(String date , String fomat) {
        SimpleDateFormat format = new SimpleDateFormat(fomat);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }


}
